//
//  RVFeedbackController.swift
//  AirTravel
//
//  Created by apple on 2021/11/23.
//

import UIKit

class RVFeedbackController: CommonVC {

    @IBOutlet weak var nameTF: UITextField!
    
    @IBOutlet weak var telTF: UITextField!
    
    @IBOutlet weak var type: UITextField!
    
    @IBOutlet weak var descTF: UITextView!
    
    @IBOutlet weak var appealTF: UITextView!
    
    @IBOutlet weak var confirmBtn: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        head.title.text = "意见反馈"
        view.backgroundColor = .white
        confUI()
        
    }

    func confUI() {
        type.delegate = self
        confirmBtn.setBtnGradient()
        descTF.layer.cornerRadius = 6
        descTF.layer.masksToBounds = true
        descTF.layer.borderWidth = 1
        descTF.layer.borderColor = UIColor(hexString: "999999").cgColor
        
        appealTF.layer.cornerRadius = 6
        appealTF.layer.masksToBounds = true
        appealTF.layer.borderWidth = 1
        appealTF.layer.borderColor = UIColor(hexString: "999999").cgColor
        descTF.placeholder = "请输入您遇到的问题"
        appealTF.placeholder = "请输入您的诉求"

    }

    @IBAction func clickCOnfirmBtn(_ sender: Any) {
        if nameTF.text?.count == 0 {
            YProgressHUD.showToast("请输入您的姓名")
            return
        }
        
        if telTF.text?.count == 0 {
            YProgressHUD.showToast("请输入您的联系方式")
            return
        }
        
        if type.text?.count == 0 {
            YProgressHUD.showToast("请选择您的投诉问题")
            return
        }
        
        if appealTF.text?.count == 0 {
            YProgressHUD.showToast("请输入您的诉求")
            return
        }
        
        var parameters: [String: Any] = [String: Any]()
        
        parameters["name"] = nameTF.text!
        parameters["phone"] = telTF.text!
        parameters["type"] = type.text!
        parameters["help"] = appealTF.text!
        if descTF.text.count > 0 {
            parameters["content"] = descTF.text!
        }


        YProgressHUD.show()
        NetWorkRequest(API1.feedBack(parameters: parameters)) { json in
            YProgressHUD.dismiss()
            YProgressHUD.showToast("投诉成功")
            self.navigationController?.popViewController(animated: true)
        } failed: { msg in
            YProgressHUD.dismiss()

        } errorResult: {
            YProgressHUD.dismiss()
        }

        
        
    }
    
}



extension RVFeedbackController: UITextFieldDelegate{
    
    
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        self.view.endEditing(true)
        showType()
        return false
    }
    
    func showType(){
        let arr = ["酒店问题", "机票问题", "门票问题", "火车票问题", "票价问题", "服务态度问题", "贵宾厅问题", "对卡片使用不满"];
        let sheet = UIAlertController(title: "请选择类型", message: nil, preferredStyle: .actionSheet)
        for str in arr {
            let action = UIAlertAction(title: str, style: .default) { ac in
                self.type.text = str
            }
            sheet.addAction(action)
        }
        
        let action = UIAlertAction(title: "取消", style: .cancel, handler: nil)
        sheet.addAction(action)
        self.present(sheet, animated: true, completion: nil)
    }
    
}
